package interceptor

import (
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
)

func AuthInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {

	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		return nil, status.Error(codes.Unauthenticated, "缺少验证信息")
	}

	var (
		user     string
		password string
		val      []string
	)
	if val, ok = md["user"]; ok {
		user = val[0]
	}
	if val, ok = md["password"]; ok {
		password = val[0]
	}
	if user != "dlwm-ctp" || password != "dlwm-ctp" {
		return nil, status.Error(codes.Unauthenticated, "验证不通过")
	}

	return handler(ctx, req)
}
